package org.lora.quartz;

import java.io.File;
import java.io.FileInputStream;
import java.text.ParseException;
import java.util.Properties;

import org.lora.util.StringUtil;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

import com.jolbox.bonecp.BoneCPConfig;
import com.jolbox.bonecp.BoneCPDataSource;

public class Test {

	/*
	 * <!-- ���п����� --> <datasource Environment="EnvironmentRunTime"> <entry
	 * key="annotationClassName"
	 * value="org.lora.biz.workflow.annotation.RunTimeResource"></entry> <entry
	 * key="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <entry
	 * key="jdbcUrl" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl" /> <entry
	 * key="username" value="bpm" /> <entry key="password" value="password" />
	 * <entry key="partitionCount" value="4" /> <entry
	 * key="maxConnectionsPerPartition" value="5" /> <entry
	 * key="minConnectionsPerPartition" value="0" /> <entry
	 * key="acquireIncrement" value="1" /> <entry
	 * key="poolAvailabilityThreshold" value="20" /> <entry
	 * key="connectionTimeout" value="3600000" /> <entry
	 * key="pageInterceptorClassName"
	 * value="org.lora.core.mybatis.PaginationInterceptor" /> <entry
	 * key="pageDialectClassName" value="org.lora.core.mybatis.OracleDialect" />
	 * </datasource>
	 */

	public static final String DEFAULT_PROPERTIE_FILE_NAME = "quartz.properties";

	public static void main(String args[]) throws SchedulerException, ParseException {

		try {

			BoneCPConfig poolConfig = new BoneCPConfig();

			poolConfig.setJdbcUrl("jdbc:oracle:thin:@127.0.0.1:1521:orcl");
			poolConfig.setUsername("bpm");
			poolConfig.setPassword("password");

			poolConfig.setAcquireIncrement(1);
			poolConfig.setPartitionCount(4);
			poolConfig.setMaxConnectionsPerPartition(5);
			poolConfig.setMinConnectionsPerPartition(0);
			poolConfig.setPoolAvailabilityThreshold(20);
			poolConfig.setDisableConnectionTracking(true);
			poolConfig.setConnectionTimeoutInMs(3600000l);

			BoneCPDataSource dataSource = new BoneCPDataSource(poolConfig);

			Properties prop = new Properties();

			String path = Test.class.getClassLoader().getResource(StringUtil.BLANK).toURI().getPath();
			FileInputStream fis = new FileInputStream(new File(path + DEFAULT_PROPERTIE_FILE_NAME));
			prop.load(fis);

			JobDetail jobDetail = JobBuilder.newJob(TestJob.class).withIdentity("testJob_1", "group_1").build();

			Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger_1", "group_1").startNow()
					.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(10) 
							.withRepeatCount(5)
					).build();
			SchedulerFactory sf = new StdSchedulerFactory(prop, dataSource,"BoneCPDataSource");
			Scheduler sched = sf.getScheduler();

			sched.scheduleJob(jobDetail, trigger);

			sched.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
